public class HashBuck {
    static class Node{
        public int key;
        public int value;
        public Node next;
        public Node(int key,int value){
            this.key=key;
            this.value=value;
        }
    }
    public Node[] array;
    public int usedSize;
    public HashBuck(){
        array=new Node[10];
    }
    public static final double Lode_factor=0.75;
    public void push(int key,int value){
          //首先找到我要插入的那个值(key)的下标
        int index=key%array.length;
        //看一下这个下标所对应值中，有没有出现过我要插入的值
        Node cur=array[index];
        while (cur!=null){
            if (cur.key==key){
                //更新当前cur的value值
                cur.value=value;
                return;
            }
            cur=cur.next;
        }
        Node node=new Node(key, value);
        //头插法
        node.next=array[index];
        array[index]=node;
        usedSize++;
        //计算此时的负载因子，看此时的负载因子是否大于默认的负载因子
        if(calcLodeFactor()>=Lode_factor){
            //扩容
            resize();
        }
    }

    private void resize() {
        //先定义一个新的数组
        Node[] newArray=new Node[2*array.length];
        //遍历array数组
        for (int i = 0; i < array.length; i++) {
            Node cur=array[i];
            while (cur!=null){
                int newIndex= cur.key% newArray.length;
                Node curN=cur.next;
                //头插法
                cur.next=newArray[newIndex];
                newArray[newIndex]=cur;
                cur=curN;
            }
        }
        array=newArray;

    }

    private double calcLodeFactor() {
        return usedSize*1.0/ array.length;
    }

    public int get(int key){
        int index=key%array.length;
        Node cur=array[index];
        while (cur!=null){
            if (cur.key==key){
                return cur.value;
            }
            cur=cur.next;
        }
        return -1;
    }
}
